
import pymysql
from pymysql.cursors import DictCursor

class DB:

    def __init__(self, config):
        self.conn = pymysql.connect(
            host=config['host'],
            user=config['user'],
            password=config['password'],
            database=config['database'],
            cursorclass=DictCursor   #设置游标的类型，查询结果会以字典形式返回
        )

    def query(self, sql):
        with self.connect.cursor() as cursor:
            cursor.execute(sql)
            return cursor.fetchone()

    def execute_sql(self, sql):
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            self.conn.commit()
    
    def __del__(self):
        self.conn.close()


